Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
Django models.Manager custom SQL query "more than one row returned" error

So I'm trying to get the result of the age() function on PostgreSQL from a column in the DB.

The goal is to append a new virtual column with the age or time elapsed since the datetime stored the database.

I tried this by creating a models.Manager in Django to add this new column:

class PriorityManager(models.Manager):
    def get_queryset(self):
        time_elapsed = RawSQL('SELECT EXTRACT(EPOCH FROM age(datetime)) AS age FROM backend_post',
                               params=(),
                               output_field=models.IntegerField())
    return super(PriorityManager, self).get_queryset().annotate(score=time_elapsed)

I need a score column to help sort the objects later, and the score is based on the time elapsed.

The query SELECT EXTRACT(EPOCH FROM age(datetime)) AS age FROM backend_post; works in the dbshell but when running it on Django, the following message comes up:

ProgrammingError at /api/post/list/ more than one row returned by a subquery used as an expression

I suspect the problem is that the query returns a whole column of elements while the models.Manager expected only one result. I believe there should be something sent as a parameter in the RawSQL query but I couldn't figure out what it is...

Anyone knows what to do?

Thank you very much!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Solved this problem by using

http://django-postgres-stats.readthedocs.io/

It allows for many custom postgres functions with django, just what I needed. Don't know why it took me so long to find it!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda